package com.zzptc.lzh.myapp12.Adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.zzptc.lzh.myapp12.Bean.Msg;
import com.zzptc.lzh.myapp12.R;

import java.util.List;

public class MsgAdapter extends ArrayAdapter<Msg> {
    int resourceid;
    public MsgAdapter(@NonNull Context context, int resource, @NonNull List<Msg> objects) {
        super(context, resource, objects);
        this.resourceid=resource;//付id并接住
    }
    @Override
    public View getView(int position, View converView, ViewGroup parent){
        View view=converView;
        if (view==null){
            view= LayoutInflater.from(getContext()).inflate(resourceid,null);
        }
        LinearLayout left_layout=view.findViewById(R.id.left_layout);
        LinearLayout right_layout=view.findViewById(R.id.right_layout);
        TextView left_msg=view.findViewById(R.id.left_msg);
        TextView right_msg=view.findViewById(R.id.right_msg);
        Msg msg=getItem(position);
        SharedPreferences sp=getContext().getSharedPreferences("userinfo",Context.MODE_PRIVATE);
        int myid=sp.getInt("myid",-1);
        if (myid==msg.getSend_id()){
            left_layout.setVisibility(View.GONE);//左边消失
            right_layout.setVisibility(View.VISIBLE);
            right_msg.setText(msg.getContent());//输出右边信息
        }else{
            right_layout.setVisibility(View.GONE);//右边消失
            left_layout.setVisibility(View.VISIBLE);
            left_msg.setText(msg.getContent());//输出左边信息
        }
        return view;
    }
}
